package com.apache.seq.service.impl;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.seq.entity.Seq;
import com.apache.seq.manager.SeqManager;
import com.apache.tools.DateUtils;

import java.util.List;

public class SeqManagerImpl implements SeqManager {

    protected IDao seqDao;

    protected final String entityName = "com.apache.seq.entity.Seq";

    public void setSeqDao(IDao seqDao) {
        this.seqDao = seqDao;
    }

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     *
     * @see com.apache.api.manager.BaseManager#saveInfo(com.apache.api.vo.ParamsVo)
     */
    public String saveInfo(ParamsVo<Seq> vo) throws BusinessException {
        Seq seq = vo.getObj();
        String infoId = Validator.generate();
        //定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
        String cacheKey = "";//"seq_" + infoId;
        if (Validator.isNull(seq.getSeqEname()))
            seq.setSeqEname(infoId);
        seq.setUpdateCount(0L);
        seq.setFirstGetTime(DateUtils.Now.fmt_yyyyMMdd_HHmmssSSS());
        MethodParam param = new MethodParam("Seq", cacheKey, "", entityName);
        param.setVaule(seq);
        if (seqDao.insert(param)) {
            return infoId;
        }
        return "";
    }

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     *
     * @see com.apache.api.manager.BaseManager#editInfo(com.apache.api.vo.ParamsVo)
     */
    public boolean editInfo(ParamsVo<Seq> vo) throws BusinessException {
        Seq seq = vo.getObj();
        if (Validator.isNotNull(seq.getSeqEname())) {
            String cacheKey = "";//"seq_" + seq.getSeqEname();
            MethodParam param = new MethodParam("Seq", cacheKey, "", entityName);
            param.setVaule(seq);
            return seqDao.edit(param);
        }
        return false;
    }

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     *
     * @see com.apache.api.manager.BaseManager#deleteInfo(com.apache.api.vo.ParamsVo)
     */
    public boolean deleteInfo(ParamsVo<Seq> vo) throws BusinessException {
        String infoId = vo.getInfoId();
        if (Validator.isNull(infoId)) {
            return false;
        }
        String cacheKey = "";//"seq_" + infoId;
        String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
        MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
        param.setInfoId(infoId);
        Seq info = (Seq) seqDao.selectById(param);
        if (Validator.isEmpty(info)) {
            return false;
        }
        param.setVaule(info);//此处需要先将状态值赋值为删除状态
        if ("false".equals(mark)) {//逻辑删除
            param.setKey("Seq");
            return seqDao.edit(param);
        } else {
            param.setParams("seqEname", infoId);
            param.setDelete(true);
            return seqDao.delete(param);
        }
    }

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     *
     * @see com.apache.api.manager.BaseManager#getInfoById(com.apache.api.vo.ParamsVo)
     */
    public Object getInfoById(ParamsVo<Seq> vo) {
        String infoId = vo.getInfoId();
        String cacheKey = "";//"seq_" + infoId;
        if (Validator.isNull(infoId)) {
            return null;
        }
        MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
        param.setInfoId(infoId);
        return seqDao.selectById(param);
    }

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     *
     * @see com.apache.api.manager.BaseManager#execute(com.apache.api.vo.ParamsVo)
     */
    public Object execute(ParamsVo<Seq> vo) {
        String key = vo.getMethodKey();
        if ("checkUnique".equals(key)) {
            String seqEname = String.valueOf(vo.getParams("seqEname"));
            return checkUnique(seqEname);
        }
        return null;
    }

    /**
     * description:  验证英文名称是否已存在
     */
    private boolean checkUnique(String seqEname) {
        if (Validator.isNull(seqEname))
            return true;
        MethodParam param = new MethodParam("BySeqEname", "",
                seqDao.getSql(1) + " and seqEname =:seqEname", entityName);
        param.setParams("seqEname", seqEname);
        long count = seqDao.count(param);
        if (count > 0)
            return true;
        return false;
    }

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     *
     * @see com.apache.seq.manager.UnityBaseManager#getPageInfo(com.apache.api.vo.ParamsVo)
     */
    public Page getPageInfo(ParamsVo<Seq> vo) {
        MethodParam param = setMethodParams(vo, 2, " seqEname desc");
        int pageSize = Integer
                .valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
        int pageIndex = Integer
                .valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
        param.setOrderby(" seq_ename desc ");
        param.setPageIndex(pageIndex);
        param.setPageSize(pageSize);
        Page page = seqDao.pageSelect(param);
        return page;
    }

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     *
     * @see com.apache.api.manager.BaseManager#getList(com.apache.api.vo.ParamsVo)
     */
    @SuppressWarnings("unchecked")
    public List<Seq> getList(ParamsVo<Seq> vo) {
        MethodParam param = setMethodParams(vo, 2, " seqEname desc");
        param.setParams("orderby", " seq_ename desc ");
        return seqDao.select(param);
    }

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     *
     * @see com.apache.api.manager.BaseManager#countInfo(com.apache.api.vo.ParamsVo)
     */
    public long countInfo(ParamsVo<Seq> vo) {
        MethodParam param = setMethodParams(vo, 1, "");
        return seqDao.count(param);
    }

    private MethodParam setMethodParams(ParamsVo<Seq> vo, int type, String orderby) {
        String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
        StringBuffer sb = new StringBuffer(seqDao.getSql(type));
        MethodParam param = new MethodParam(methodKey, "", "", entityName);
        String seqEname = String.valueOf(vo.getParams("seqEname"));
        String seqCname = String.valueOf(vo.getParams("seqCname"));

        if (Validator.isNotNull(seqEname)) {
            sb.append(" and seqEname like '%" + seqEname + "%'");
            param.setParams("seqEname", "'%" + seqEname + "%'");
        }
        if (Validator.isNotNull(seqCname)) {
            sb.append(" and seqCname like '%" + seqCname + "%'");
            param.setParams("seqCname", "'%" + seqCname + "%'");
        }
        if (Validator.isNotNull(orderby)) {
            sb.append(" order by " + orderby);
        }
        param.setSqlStr(sb.toString());
        return param;
    }
}
